{
 "metadata": {
  "name": "",
  "signature": "sha256:3a5f21a87cda15097167c53e6467f5007cb587cc4eb7f41de096a7c4bc03cb13"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Zhenfeng Liang"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# MTH9879 Homework 7\n",
      "\n",
      "Assigned: March 31, 2015.\n",
      "Due: April 14, 2015 by 6pm. \n",
      "\n",
      "Late homework **will not be accepted**.\n",
      "\n",
      "$$\n",
      "\\newcommand{\\supp}{\\mathrm{supp}}\n",
      "\\newcommand{\\E}{\\mathbb{E}}\n",
      "\\newcommand{\\Eof}[1]{\\mathbb{E}\\left[ #1 \\right]}\n",
      "\\def\\Cov{{ \\mbox{Cov} }}\n",
      "\\def\\Var{{ \\mbox{Var} }}\n",
      "\\newcommand{\\1}{\\mathbf{1} }\n",
      "\\newcommand{\\PP}{\\mathbb{P} }\n",
      "%\\newcommand{\\Pr}{\\mathrm{Pr} }\n",
      "\\newcommand{\\QQ}{\\mathbb{Q} }\n",
      "\\newcommand{\\RR}{\\mathbb{R} }\n",
      "\\newcommand{\\DD}{\\mathbb{D} }\n",
      "\\newcommand{\\HH}{\\mathbb{H} }\n",
      "\\newcommand{\\spn}{\\mathrm{span} }\n",
      "\\newcommand{\\cov}{\\mathrm{cov} }\n",
      "\\newcommand{\\sgn}{\\mathrm{sgn} }\n",
      "\\newcommand{\\HS}{\\mathcal{L}_{\\mathrm{HS}} }\n",
      "%\\newcommand{\\HS}{\\mathrm{HS} }\n",
      "\\newcommand{\\trace}{\\mathrm{trace} }\n",
      "\\newcommand{\\LL}{\\mathcal{L} }\n",
      "%\\newcommand{\\LL}{\\mathrm{L} }\n",
      "\\newcommand{\\s}{\\mathcal{S} }\n",
      "\\newcommand{\\ee}{\\mathcal{E} }\n",
      "\\newcommand{\\ff}{\\mathcal{F} }\n",
      "\\newcommand{\\hh}{\\mathcal{H} }\n",
      "\\newcommand{\\bb}{\\mathcal{B} }\n",
      "\\newcommand{\\dd}{\\mathcal{D} }\n",
      "\\newcommand{\\g}{\\mathcal{G} }\n",
      "\\newcommand{\\p}{\\partial}\n",
      "\\newcommand{\\half}{\\frac{1}{2} }\n",
      "\\newcommand{\\T}{\\mathcal{T} }\n",
      "\\newcommand{\\bi}{\\begin{itemize}}\n",
      "\\newcommand{\\ei}{\\end{itemize}}\n",
      "\\newcommand{\\beq}{\\begin{equation}}\n",
      "\\newcommand{\\eeq}{\\end{equation}}\n",
      "\\newcommand{\\beas}{\\begin{eqnarray*}}\n",
      "\\newcommand{\\eeas}{\\end{eqnarray*}}\n",
      "\\newcommand{\\cO}{\\mathcal{O}}\n",
      "\\newcommand{\\cF}{\\mathcal{F}}\n",
      "\\newcommand{\\cL}{\\mathcal{L}}\n",
      "\\newcommand{\\BS}{\\text{BS}}\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "<font color = \"red\">Homework is to be done by each student individually.  To receive full credit, you must email a completed copy of this iPython notebook to Yu Gan (yugan323@gmail.com), Fubo Shi (fubo.shi.baruch@gmail.com), and Tai-Ho Wang (tai-ho.wang@baruch.cuny.edu) by the due date and time.  All R-code must run correctly and solutions must be written up neatly in Markdown/LaTeX format.\n",
      "\n",
      "<font color=\"blue\">If you encounter problems with Markdown/LaTeX or iPython notebook, please contact your TAs Yu Gan and/or Fubo Shi.\n"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 0. (0 points) \n",
      "Read Sections 4, 5 and 6 of Bouchaud, Farmer and Lillo."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Long memory"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 1. (6 points)\n",
      "\n",
      "Consider the long memory process\n",
      "\n",
      "$$\n",
      "X_T=\\sum_{i=1}^T\\,\\epsilon_i\n",
      "$$\n",
      "\n",
      "with autocovariance function $\\gamma(\\tau)=\\E[\\epsilon_i\\,\\epsilon_{i-\\tau}] \\sim \\tau^{-\\alpha}$.  Show that as $\\tau \\to \\infty$,\n",
      "\n",
      "$$\n",
      "\\Var[X_{t+\\tau}-X_t] \\sim \\tau ^{2\\,H}\n",
      "$$\n",
      "\n",
      "where the Hurst exponent $H$ is given by\n",
      "\n",
      "$$\n",
      "H=1-\\frac{\\alpha}{2}.\n",
      "$$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Solution\n",
      "\n",
      "Because $\\gamma(\\tau)=\\E[\\epsilon_i\\,\\epsilon_{i-\\tau}] \\sim \\tau^{-\\alpha}$, $\\Var[\\epsilon_i] = 0$\n",
      "\n",
      "\\begin{eqnarray}\n",
      "\\Var[X_{t+\\tau}-X_t] &=& \\Var[\\sum_{i=t+1}^{t + \\tau}\\,\\epsilon_i] \\\\\n",
      "&=& \\sum_{i,j = t+1}^{t + \\tau} \\Cov(\\epsilon_i ,\\, \\epsilon_j) \\\\\n",
      "&\\sim& 2 \\sum_{i = 1}^{\\tau - 1} (\\tau - i)i^{- \\alpha} \\\\\n",
      "&\\approx& 2\\int_{1}^{\\tau - 1}(\\tau - x)x^{- \\alpha}dx \\\\\n",
      "&=& \\frac{2\\,\\tau}{1 - \\alpha} ((\\tau - 1)^{1 - \\alpha} - 1) - \\frac{2}{2 - \\alpha}((\\tau - 1)^{2-\\alpha} - 1) \\\\\n",
      "&\\sim& \\tau^{2 - \\alpha} \\\\\n",
      "&\\sim& \\tau^{2H}  \n",
      "\\end{eqnarray}\n",
      "where $H = 1 - \\frac{\\alpha}{2}$"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## MSFT data from Alejandro Ca&ntilde;ete"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Set up R environment"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%load_ext rmagic"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "library(highfrequency)\n",
      "download.file(url=\"http://mfe.baruch.cuny.edu/wp-content/uploads/2015/03/MSFT130311.rData_.zip\", destfile=\"MSFT130311.zip\")\n",
      "unzip(zipfile=\"MSFT130311.zip\")\n",
      "load(\"MSFT130311.rData\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "Loading required package: xts\n",
        "Loading required package: zoo\n",
        "\n",
        "Attaching package: \u2018zoo\u2019\n",
        "\n",
        "The following objects are masked from \u2018package:base\u2019:\n",
        "\n",
        "    as.Date, as.Date.numeric\n",
        "\n",
        "trying URL 'http://mfe.baruch.cuny.edu/wp-content/uploads/2015/03/MSFT130311.rData_.zip'\n",
        "Content type 'application/zip' length 71919 bytes (70 Kb)\n",
        "opened URL\n",
        "==================================================\n",
        "downloaded 70 Kb\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 2. (6 points)\n",
      "\n",
      "(a) Generate the series *tradeSign* of trade signs from the *msft130311.inet* data frame.\n",
      "\n",
      "(b) Fit an AR(1) model to the *tradeSign*  data to give the forecast:\n",
      "    $$\n",
      "    \\hat \\epsilon^{(1)}_t=\\phi_1\\,\\epsilon_{t-1}\n",
      "    $$\n",
      "\n",
      "(c) How does $\\phi_1$ relate to the autocorrelation coefficients of *tradeSign*?  What is the mean-squared error of this AR(1) forecast?\n",
      "\n",
      "(d) Fit an $AR(p)$ model to the *tradeSign* data, allowing R to find the best value of $p$ using the default AIC criterion.  This generates the forecast:\n",
      "    $$\n",
      "    \\hat \\epsilon^{(2)}_t=\\sum_{k=1}^p\\,\\phi_k\\,\\epsilon_{t-k}.\n",
      "    $$\n",
      "\n",
      "(e) What value of $p$ is selected by R?  What is the mean-squared error of this AR(p) forecast?\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (a)\n",
      "ts.inet <- sign(msft130311.inet$signed.shares)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 51
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (b)\n",
      "fit.inet <- lm(ts.inet[2:length(ts.inet)]~ts.inet[1:length(ts.inet)-1])\n",
      "phi1 <- fit.inet$coefficients[2]\n",
      "\n",
      "tradeSignForecast <- function(formerSign){\n",
      "    return (phi1 * formerSign)\n",
      "}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 52
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (c)\n",
      "# Define a function to calculate mean-squared error \n",
      "MSE <- function(residualsVec){\n",
      "    return (mean(residualsVec^2))\n",
      "}\n",
      "\n",
      "CalMSE <- function(lmResult){\n",
      "    sm <- summary(lmResult)\n",
      "    mse <- MSE(sm$residuals)\n",
      "    return(mse)\n",
      "}\n",
      "\n",
      "# Use functions to do part c\n",
      "acts.inet <- acf(ts.inet, plot=F)\n",
      "cat(\"Autocorrelation coefficients of tradeSign where lag = 1 is\", acts.inet$acf[2])\n",
      "cat(\"\\nPhi1 is\", phi1)\n",
      "\n",
      "# Calculate MSE\n",
      "mse <- CalMSE(fit.inet)\n",
      "cat(\"\\nThe mean-squared error of AR(1) is\", mse)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "Autocorrelation coefficients of tradeSign where lag = 1 is 0.8210177\n",
        "Phi1 is 0.8210784\n",
        "The mean-squared error of AR(1) is 0.3234549"
       ]
      }
     ],
     "prompt_number": 53
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### We could see that $\\phi$ equals to the autocorrelation coefficients of tradeSign"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (d)\n",
      "AR_p <- function(p, tradeSign){\n",
      "\n",
      "    len <- length(tradeSign)\n",
      "    listoffactors <- c()\n",
      "    for (i in p:1){\n",
      "        offset <- 1+p-i\n",
      "        offset <- len - offset\n",
      "        newVar <- paste(\"tradeSign[\", deparse(i),\":\", deparse(offset), \"]\")\n",
      "        listoffactors <- c(listoffactors, newVar)\n",
      "    }\n",
      "    \n",
      "    yVar <- paste(\"tradeSign[\", deparse(1+p),\":\",deparse(len),\"]\")\n",
      "    formula <- as.formula(paste(yVar,\"~\",paste(listoffactors,collapse=\"+\")))\n",
      "    return (lm(formula))\n",
      "}\n",
      "\n",
      "SelectModel_AIC <- function(tradeSign, numVariTrials){\n",
      "    \n",
      "    min <- Inf\n",
      "    k <- 0\n",
      "    for(i in 1:numVariTrials){\n",
      "        ar_i <- AR_p(i, tradeSign)\n",
      "        aic <- AIC(ar_i, k = i)\n",
      "        \n",
      "        if (aic < min){\n",
      "            k <- i\n",
      "            min <- aic\n",
      "        }\n",
      "    }\n",
      "    return (k)\n",
      "}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 54
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (e)\n",
      "numVariTrials <- 20\n",
      "\n",
      "selectedNumVariable <- SelectModel_AIC(ts.inet, numVariTrials)\n",
      "#selectedNumVariable <- 14\n",
      "fitted_model <- AR_p(selectedNumVariable, ts.inet)\n",
      "aic <- AIC(fitted_model, k = selectedNumVariable)\n",
      "cat(\"Best fitted model's p under 1 :\", numVariTrials, \"trials is: \", selectedNumVariable)\n",
      "cat(\"\\nThe best fitted model's AIC is: \", aic)\n",
      "\n",
      "fitted_mse <- CalMSE(fitted_model)\n",
      "cat(\"\\nThe MSE of this AR(p) model is: \", fitted_mse)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "Best fitted model's p under 1 : 20 trials is:  8\n",
        "The best fitted model's AIC is:  26929.67\n",
        "The MSE of this AR(p) model is:  0.3119226"
       ]
      }
     ],
     "prompt_number": 60
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### 3. (8 points)\n",
      "\n",
      "(a) Bin the AR(p) forecast data from the previous problem by forecast sign with bin-breaks from -1.01 to 1.01 in steps of 0.02.\n",
      "\n",
      "(b) Plot the average actual order sign in each bin against the average forecast order sign.  What is the slope of the resulting graph? (Hint: use the R function *lm*).\n",
      "\n",
      "(c) If the expected order sign is $\\hat \\epsilon$, what is the probability $p$ that the next trade will be a market buy?"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (a) \n",
      "\n",
      "fittedValue <- fitted(fitted_model)\n",
      "\n",
      "# Assign the bin value\n",
      "x <- c()\n",
      "temp <- -1.01\n",
      "for(i in 1:102){\n",
      "    x <- c(x, temp)\n",
      "    temp = temp + 0.02\n",
      "}\n",
      "\n",
      "# Plot histogram\n",
      "hist(fittedValue, breaks = x, main = \"Histogram of forecast data\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAAC+lBMVEUAAAABAQECAgIDAwMEBAQF\nBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcY\nGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJycoKCgpKSkqKior\nKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6Ojo7Ozs8PDw9PT0+\nPj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tMTExNTU1OTk5PT09QUFBR\nUVFSUlJTU1NUVFRV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vC+Z\n12xl9nlLqYUB58znvOpQ9dInxL9UB/BrqU8u4+9aTvA3ncAWznOTec1WZp+3lFoQcLn4dbTrzooV\nyXxPxL4Tk8LFqp869Pycrx11cp9l4lUBXNTmy/JxybxmKxK1HOA7Q8+JXx8P6jD+IOdro6I2RDnW\nzexVzcvTOoz8OPYtAVy9pGPCu0K2ZisKtQTgFh2AiQdg4gGYeAAmHoCJB2DiAZh4ACYegIkHYOIB\nmHgAJh6AiQdg4gGYeAAmHoCJB2Di/T9s8nd6/eXFhgAAAABJRU5ErkJggg==\n"
      }
     ],
     "prompt_number": 57
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### A function to calculate the average fitted order sign and average actual order sign in each bin\n",
      "CalAverageOrderSign <- function(actualOrderSign, fittedOrderSign){\n",
      "    \n",
      "    leftBound <- -1.01\n",
      "    len <- length(fittedOrderSign)\n",
      "    \n",
      "    actualAverage <- c()\n",
      "    fittedAverage <- c()\n",
      "    \n",
      "    for(i in 1:101){\n",
      "        rightBound <- leftBound + 0.02\n",
      "        numOfObs <- 0\n",
      "        sumFitted <- 0\n",
      "        sumActual <- 0\n",
      "        \n",
      "        for(j in 1:len)\n",
      "        {\n",
      "            temp <- fittedOrderSign[j]            \n",
      "            if((temp >= leftBound) && (temp < rightBound))\n",
      "            {\n",
      "                sumFitted = sumFitted + temp\n",
      "                sumActual = sumActual + actualOrderSign[j]\n",
      "                numOfObs = numOfObs + 1\n",
      "            }\n",
      "        }\n",
      "        fittedAveTemp <- 0\n",
      "        actualAveTemp <- 0\n",
      "        \n",
      "        if(numOfObs != 0){\n",
      "            fittedAveTemp <- sumFitted/numOfObs\n",
      "            actualAveTemp <- sumActual/numOfObs            \n",
      "        }\n",
      "\n",
      "        fittedAverage <- c(fittedAverage, fittedAveTemp)\n",
      "        actualAverage <- c(actualAverage, actualAveTemp)\n",
      "        leftBound <- rightBound\n",
      "    }\n",
      "    return(list(forecastAveOrderSign = fittedAverage, actualAveOrderSign = actualAverage))   \n",
      "}\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 58
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%R\n",
      "\n",
      "### (b) \n",
      "\n",
      "# Select the effective fitted order sign observations\n",
      "fittedOrderSign <- fittedValue\n",
      "\n",
      "# Select the effective actual order sign observations\n",
      "begin <- 1+selectedNumVariable\n",
      "actualOrderSign <- ts.inet[begin:len]\n",
      "\n",
      "# Calculate the average in each bin\n",
      "VecList <- CalAverageOrderSign(actualOrderSign, fittedOrderSign)\n",
      "\n",
      "# Extract the average result\n",
      "forecastAveOrderSign <- VecList$forecastAveOrderSign\n",
      "actualAveOrderSign <- VecList$actualAveOrderSign\n",
      "\n",
      "# Plot the graph\n",
      "plot(forecastAveOrderSign, actualAveOrderSign)\n",
      "fit <- lm(actualAveOrderSign~forecastAveOrderSign)\n",
      "abline(fit)\n",
      "\n",
      "# Output the slope\n",
      "sm <- summary(fit)\n",
      "cat(\"The slope between the average actual order sign and the average fitted order sign is\", sm$coefficients[2,1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "display_data",
       "text": [
        "The slope between the average actual order sign and the average fitted order sign is 0.964222"
       ]
      },
      {
       "metadata": {},
       "output_type": "display_data",
       "png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAADAFBMVEUAAAABAQECAgIDAwMEBAQF\nBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcY\nGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJycoKCgpKSkqKior\nKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6Ojo7Ozs8PDw9PT0+\nPj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZH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YgbjeOu26vSCur+lTv9hi5/XEbnC4\nxYHzfvM+9r6yr7Ou8v1OOec6L4FGBVsfM3Iq3VPgVJvWevOuMNl9bfb+aI8i/u17mu/LC3UzV/El\nec0T/7q46ke9LgIAAADDSURBVL5j3Hrm/x3KDUTBDsf1FqzxaPb85apPfcuTTQJPcye8j94XV1YQ\n1MRri/vXNyOb/mtUOQxs1v9Vfv8J3A9sh9E1uxq/7NLU4+UbVW//uE2bpW3MVaaSQyFuzbw2mdYX\ntvT49ikUzCznjgEcDFK6FcJAwQLY5XGxfMD7SrdCGChYCLP/r+XgW0o3QhgomHFQMOOgYMZBwYyD\nghkHBTMOCmYcFMw4KJhxUDDjoGDGQcGMg4IZBwUzDgpmHBTMOP8PmpIcHcAXWCwAAAAASUVORK5C\nYII=\n"
      }
     ],
     "prompt_number": 59
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\n",
      "### Solution (c)\n",
      "\n",
      "As we know,\n",
      "\n",
      "\\begin{eqnarray}\n",
      "\\hat \\epsilon_t &=& \\mathbb{E} [\\epsilon_t|\\mathcal{F} _{t-1}] \\\\\n",
      "&=& p + (1 - p) * (-1)\n",
      "&=& 2p - 1\n",
      "\\end{eqnarray}\n",
      "\n",
      "So, $p = (\\hat \\epsilon_t + 1)/2$"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}